home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / multitasking / feature / executive_v2.10 / sysinfo / oberon / source / sysinfotest.mod < prev    next >
Text File  |  2000-03-02  |  10KB  |  265 lines

  1. (*(*
  2.  * Test-program for some SysInfo.library features
  3.  *
  4.  * This file is public domain.
  5.  *
  6.  * Author: Petri Nordlund <petrin@megabaud.fi>
  7.  *
  8.  * $Id: test.c 1.3 1995/10/12 16:31:32 petrin Exp petrin $
  9.  *
  10.  * Oberon-Version by Thomas Igracki (T.Igracki@Jana.berlinet.de)
  11.  *
  12.  * 20.10.96: - Recompiled for v2
  13.  *           - Fixed a little bug: Doesn't printed pgrp/ppid unknown if unknown
  14.  *)*)
  15.  
  16. MODULE SysInfoTest;
  17. IMPORT
  18.   y: SYSTEM, e: Exec, d: Dos, u: Utility, 
  19.   si: SysInfo, rc: RealConversions;
  20.  
  21. TYPE
  22.    LS = LONGSET;
  23. VAR
  24.    p,
  25.    nice : LONGINT;
  26.    
  27.    not  : si.NotifyPtr;
  28.    msg  : e.MessagePtr;
  29.    i    : SHORTINT;
  30.    info : si.SysInfoPtr;
  31.    load : si.LoadAverage;
  32.    cu   : si.CpuUsage;
  33.    tcu  : si.TaskCpuUsage;
  34.    
  35. PROCEDURE WriteReal (r: REAL; v,n: INTEGER);
  36. VAR str: ARRAY 50 OF CHAR;
  37. BEGIN
  38.      IF rc.RealToString (r, str, v,n, FALSE) THEN d.PrintF (str) END;
  39. END WriteReal;
  40.  
  41. PROCEDURE PutS (str: ARRAY OF CHAR); (* $CopyArrays- *)
  42. BEGIN     d.PrintF ("%s\n",y.ADR(str))
  43. END PutS;
  44.  
  45. BEGIN
  46.      IF si.base = NIL THEN d.PrintF ("Failed to open %s v%ld!\n",y.ADR(si.name),si.version); HALT(d.warn) END;
  47.  
  48.      (* Initialize SysInfo.library, this will make the connection to the
  49.       * server-process and allocate the SysInfo-structure. *)
  50.  
  51.      info := si.InitSysInfo();
  52.      IF info = NIL THEN PutS("Couldn't initialize SysInfo."); HALT(d.fail) END;
  53.  
  54.      (* print our pid, ppid and pgrp *)
  55.      d.PrintF("pid:  %ld\n", si.GetPid(info));
  56.      IF info.getPpidImplemented THEN
  57.         p := si.GetPpid(info);
  58.         IF (p # -1) THEN d.PrintF("ppid: %ld\n",p)
  59.                     ELSE d.PrintF("ppid: unknown\n") END;
  60.      END;
  61.      IF info.getPgrpImplemented THEN
  62.         p := si.GetPgrp(info);
  63.         IF (p # -1) THEN d.PrintF("pgrp: %ld\n",p)
  64.                     ELSE d.PrintF("pgrp: unknown\n") END;
  65.      END;
  66.  
  67.      (* If info.whichImplemented is 0, then GetNice() and SetNice() are not available *)
  68.      (* We'll also make sure that the search methods we need have been implemented    *)
  69.      IF ({si.whichPrioTask,si.whichPrioProcess} * info.whichImplemented # {}) THEN
  70.         (* display the nice-value for this task *)
  71.         nice := si.GetNice (info, si.whichPrioTask, 0);
  72.         IF (nice = -1) THEN
  73.            IF (info.errNo # 0) THEN
  74.               d.PrintF("GetNice() failed, errno: %ld\n",info.errNo);
  75.            ELSE
  76.               d.PrintF("nice: %ld\n",nice);
  77.            END;
  78.         ELSE
  79.            d.PrintF("nice: %ld\n",nice);
  80.  
  81.            (* set our nice-value to +5 *)
  82.            IF si.SetNice (info, LS{si.whichPrioProcess},si.GetPid(info),5) # 0 THEN
  83.               d.PrintF("SetNice() failed, errno: %ld\n",info.errNo);
  84.            END
  85.         END;
  86.      END;
  87.  
  88.  
  89.      (* Ask for notify and output load averages every second for 10 seconds. *)
  90.  
  91.      IF (info.loadAvgType # si.loadAvgNone) THEN
  92.         IF info.notifyMsgImplemented THEN not := si.AddNotify(info,{si.anUseMessages},10) END;
  93.         IF not.port.sigBit < 0 THEN d.PrintF ("Notify.port.sigBit is < 0!\n") END;
  94.         IF (not # NIL) & (not.port.sigBit >= 0) THEN
  95.            d.PrintF("load averages (%ld.%02ld, %ld.%02ld, %ld.%02ld minutes):\n",
  96.                     info.loadAvgTime1 DIV 60, info.loadAvgTime1 MOD 60,
  97.                     info.loadAvgTime2 DIV 60, info.loadAvgTime2 MOD 60,
  98.                     info.loadAvgTime3 DIV 60, info.loadAvgTime3 MOD 60);
  99.  
  100.            FOR i := 0 TO 9 DO
  101.                (* We'll get a message every second. There may be more than
  102.                 * one message in the port at once. *)
  103.  
  104.                IF e.Wait(LONGSET{not.port.sigBit}) = LONGSET{} THEN END;
  105.                LOOP
  106.                   msg := e.GetMsg(not.port); IF msg = NIL THEN EXIT END;
  107.  
  108.                   e.ReplyMsg(msg);
  109.  
  110.                   si.GetLoadAverage(info, load);      (* Ask SysInfo.library for current load averages *)
  111.  
  112.                   d.PrintF("load average:");
  113.  
  114.                   IF info.loadAvgType = si.loadAvgFixedPnt THEN
  115.                      (* Convert fixed point values to floating point values *)
  116.                      IF info.loadAvgTime1 # 0 THEN WriteReal (load.fixed.load1 / info.fScale, 1, 2) ELSE d.PrintF(" N/A") END;
  117.                      IF info.loadAvgTime2 # 0 THEN WriteReal (load.fixed.load2 / info.fScale, 1, 2) ELSE d.PrintF(" N/A") END;
  118.                      IF info.loadAvgTime3 # 0 THEN WriteReal (load.fixed.load3 / info.fScale, 1, 2) ELSE d.PrintF(" N/A") END;
  119.                      d.PrintF("\n");
  120.                   END;
  121.                END; (* LOOP *)
  122.            END; (* FOR *)
  123.            si.RemoveNotify(info,not);
  124.         ELSE
  125.             d.PrintF("Can't use notification.\n");
  126.         END;
  127.      ELSE
  128.         d.PrintF("Load averages are not supported.\n");
  129.      END;
  130.  
  131.      (* output cpu usage values *)
  132.  
  133.      si.GetCpuUsage(info,cu);
  134.  
  135.      d.PrintF("cpu time:                 ");
  136.      IF (si.cpuUsageTotalImplemented IN info.cpuUsageImplemented) THEN
  137.         d.PrintF("%ld seconds used, %ld seconds idle\n",cu.totalUsedCpuTime, cu.totalElapsedTime - cu.totalUsedCpuTime);
  138.      ELSE
  139.         d.PrintF("N/A\n");
  140.      END;
  141.  
  142.      d.PrintF("cpu usage:                ");
  143.      IF (si.cpuUsageTotalImplemented IN info.cpuUsageImplemented) THEN
  144.         WriteReal ((cu.totalUsedCpuTime * 100) / cu.totalElapsedTime, 2,2);
  145.         d.PrintF ("%%\n");
  146.      ELSE
  147.         d.PrintF("N/A\n");
  148.      END;
  149.  
  150.      d.PrintF("current cpu usage:        ");
  151.      IF (si.cpuUsageLastsecImplemented IN info.cpuUsageImplemented) THEN
  152.         WriteReal ((cu.usedCpuTimeLastsec * 100) / cu.usedCpuTimeLastsecHz, 2,2);
  153.         d.PrintF ("%%\n");
  154.      ELSE
  155.         d.PrintF("N/A\n");
  156.      END;
  157.  
  158.      d.PrintF("recent cpu usage:         ");
  159.      IF (si.cpuUsageRecentImplemented IN info.cpuUsageImplemented) THEN
  160.         WriteReal ((cu.recentUsedCpuTime * 100) / cu.recentUsedCpuTimeHz, 2,2);
  161.         d.PrintF ("% (%ld seconds)\n", cu.recentSeconds);
  162.      ELSE
  163.         d.PrintF("N/A\n");
  164.      END;
  165.  
  166.      d.PrintF("context switches:         ");
  167.      IF (si.cpuUsageIvvoCSwImplemented IN info.cpuUsageImplemented) THEN
  168.         d.PrintF("%ld involuntary, %ld voluntary\n", cu.involuntaryCSw, cu.voluntaryCSw);
  169.      ELSE
  170.         d.PrintF("N/A\n");
  171.      END;
  172.  
  173.      d.PrintF("total context switches:   ");
  174.      IF (si.cpuUsageTotalCSwImplemented IN info.cpuUsageImplemented) THEN
  175.         d.PrintF("%ld\n", cu.totalCSw);
  176.      ELSE
  177.         d.PrintF("N/A\n");
  178.      END;
  179.  
  180.      d.PrintF("current context switches: ");
  181.      IF (si.cpuUsageIvvoCSwLastsecImplemented IN info.cpuUsageImplemented) THEN
  182.         d.PrintF("%ld involuntary, %ld voluntary\n", cu.involuntaryCSwLastsec, cu.voluntaryCSwLastsec);
  183.      ELSE
  184.         d.PrintF("N/A\n");
  185.      END;
  186.  
  187.      d.PrintF("current total csws:       ");
  188.      IF (si.cpuUsageTotalCSwLastsecImplemented IN info.cpuUsageImplemented) THEN
  189.         d.PrintF("%ld\n", cu.totalCSwLastsec);
  190.      ELSE
  191.         d.PrintF("N/A\n");
  192.      END;
  193.  
  194.  
  195.      (* output cpu usage values for this task *)
  196.  
  197.      IF si.GetTaskCpuUsage(info,tcu,NIL) = 0 THEN
  198.         d.PrintF("This task:\n");
  199.  
  200.         d.PrintF("cpu time:                   ");
  201.         IF (si.taskCpuUsageTotalImplemented IN info.taskCpuUsageImplemented) THEN
  202.            d.PrintF("%ld.%ld seconds\n",tcu.totalUsedCpuTime DIV tcu.totalUsedTimeHz, tcu.totalUsedCpuTime MOD tcu.totalUsedTimeHz);
  203.         ELSE
  204.            d.PrintF("N/A\n");
  205.         END;
  206.  
  207.         d.PrintF("cpu usage:                  ");
  208.         IF (si.taskCpuUsageTotalImplemented IN info.taskCpuUsageImplemented) THEN
  209.            WriteReal ((tcu.totalUsedCpuTime / tcu.totalUsedTimeHz * 100) / tcu.totalElapsedTime, 2,2);
  210.            d.PrintF("%%\n");
  211.         ELSE
  212.            d.PrintF("N/A\n");
  213.         END;
  214.  
  215.         d.PrintF("current cpu usage:          ");
  216.         IF (si.taskCpuUsageLastsecImplemented IN info.taskCpuUsageImplemented) THEN
  217.            WriteReal ((tcu.usedCpuTimeLastsec * 100) / tcu.usedCpuTimeLastsecHz, 2,2);
  218.            d.PrintF("%%\n");
  219.         ELSE
  220.            d.PrintF("N/A\n");
  221.         END;
  222.  
  223.         d.PrintF("recent cpu usage:           ");
  224.         IF (si.taskCpuUsageRecentImplemented IN info.taskCpuUsageImplemented) THEN
  225.            WriteReal ((tcu.recentUsedCpuTime * 100) / tcu.recentUsedCpuTimeHz, 2,2);
  226.            d.PrintF ("%% (%ld seconds)\n", tcu.recentSeconds);
  227.         ELSE
  228.            d.PrintF("N/A\n");
  229.         END;
  230.  
  231.         d.PrintF("context switches:           ");
  232.         IF (si.taskCpuUsageIvvoCSwImplemented IN info.taskCpuUsageImplemented) THEN
  233.            d.PrintF("%ld involuntary, %ld voluntary\n", tcu.involuntaryCSw, tcu.voluntaryCSw);
  234.         ELSE
  235.            d.PrintF("N/A\n");
  236.         END;
  237.  
  238.         d.PrintF("total context switches:     ");
  239.         IF (si.taskCpuUsageTotalCSwImplemented IN info.taskCpuUsageImplemented) THEN
  240.            d.PrintF("%ld\n", tcu.totalCSw);
  241.         ELSE
  242.            d.PrintF("N/A\n");
  243.         END;
  244.  
  245.         d.PrintF("context switches (ps):      ");
  246.         IF (si.taskCpuUsageIvvoCSwLastsecImplemented IN info.taskCpuUsageImplemented) THEN
  247.            d.PrintF("%ld involuntary, %ld voluntary\n", tcu.involuntaryCSwLastsec, tcu.voluntaryCSwLastsec);
  248.         ELSE
  249.            d.PrintF("N/A\n");
  250.         END;
  251.  
  252.         d.PrintF("total context switches (ps):");
  253.         IF (si.taskCpuUsageTotalCSwLastsecImplemented IN info.taskCpuUsageImplemented) THEN
  254.            d.PrintF("%ld\n", tcu.totalCSwLastsec);
  255.         ELSE
  256.            d.PrintF("N/A\n");
  257.         END;
  258.      ELSE
  259.          d.PrintF("Can't get CPU usage for this task.\n");
  260.      END;
  261.  
  262. CLOSE
  263.      IF info # NIL THEN si.FreeSysInfo(info) END;
  264. END SysInfoTest.
  265.